<template>
	<view>
		<view style="margin: 20rpx;background-color: #fff;border-radius: 20rpx;">
			<view style="padding: 30rpx;display: flex;">
				<view style="width: 80rpx;height: 80rpx;border-radius: 50%;overflow: hidden;margin-right: 20rpx;">
					<image style="width: 100%;height: 100%;" :src="'http://localhost:8080'+parentComment.avatar">
					</image>
				</view>
				<view style="flex: 1;">
					<view style="height: 80rpx;display: flex;align-items: center;justify-content: space-between;">
						<view style="display: flex;flex-direction: column;width: 50%;">
							<view style="font-size: 26rpx;">
								{{parentComment.nickname}}
							</view>
							<view style="font-size: 26rpx;color: #909399;">{{parentComment.createTime}}</view>
						</view>
					</view>
					<view>
						<text style="font-size: 26rpx;">{{parentComment.content}}</text>
					</view>
				</view>
			</view>
			<u-line></u-line>
			<view style="padding: 30rpx;">
				<view style="margin-bottom: 20rpx;">
					<text
						style="font-weight: bold;color: #20C3FE;border-bottom: 4rpx solid #20C3FE;font-size: 32rpx;">评论区</text>
				</view>
				<view style="display: flex;margin-bottom: 20rpx;" v-for="(item,index) in childCommentList" :key="index">
					<view style="width: 80rpx;height: 80rpx;border-radius: 50%;overflow: hidden;margin-right: 20rpx;">
						<image style="width: 100%;height: 100%;" :src="'http://localhost:8080'+item.avatar"></image>
					</view>
					<view style="flex: 1;">
						<view style="height: 80rpx;display: flex;align-items: center;justify-content: space-between;">
							<view style="display: flex;flex-direction: column;width: 50%;">
								<view style="font-size: 26rpx;">
									{{item.nickname}}
								</view>
								<view style="font-size: 26rpx;color: #909399;">{{item.createTime}}</view>
							</view>
							<view style="display: flex;width: 50%;align-items: center;justify-content: flex-end;">
								<view @click="clickCommentZan(item)" style="display: flex;">
									<u-icon name="thumb-up" :color="item.updateBy?'#20C3FE':'#909399'"></u-icon>
									<text style="color: #909399;">{{item.up}}</text>
								</view>
								<view @click="clickCommentCai(item)" style="display: flex;margin-left: 20rpx;">
									<u-icon name="thumb-down" :color="item.createBy?'#20C3FE':'#909399'"></u-icon>
									<text style="color: #909399;">{{item.down}}</text>
								</view>
							</view>
						</view>
						<view>
							<text style="font-size: 26rpx;">{{item.content}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>


		<!-- 评论输入 -->
		<view
			style="padding: 10rpx 0;display: flex;width: 100%;height: 80rpx;position: fixed;bottom: 0;background-color: #fff;align-items: center;border-top: 2rpx solid #d6d7d9;">
			<view style="padding: 0 20rpx;flex: 1;">
				<u-input v-model="writeContent" type="text" placeholder="说点好听的..." confirmType="评论"
					@confirm="writeComment"></u-input>
			</view>
		</view>
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		getChildCommentByPId,
		writeComment,
		getActivityUserTwoStatus,
		societyCommentZanCancel,
		societyCommentZan,
		societyCommentCaiCancel,
		societyCommentCai
	} from '../../config/api.js'
	export default {
		data() {
			return {
				id: '', //父评论id
				writeContent: '',
				parentComment: {},
				childCommentList: [],
				status: {},
				avatar: '',
				isWho: ''
			}
		},
		onLoad(options) {
			this.id = options.id;
			this.isWho = options.isWho

			this.init();
		},
		methods: {
			// 用户头像
			avatarImg(image) {
				if (image.indexOf('http') != -1) {
					console.log('包含http');
					this.avatar = image
				} else {
					this.avatar = 'http://localhost:8080' + image
				}
			},
			init() {
				// 查询子评论列表
				getChildCommentByPId({
					id: this.id,
					pid: this.id,
					isWho: this.isWho
				}).then(res => {
					this.parentComment = res.data.data.parentComment
					this.childCommentList = res.data.data.childCommentList
					this.avatarImg(res.data.data.parentComment.avatar)
					getActivityUserTwoStatus({
						pid: this.id
					}).then(res => {
						this.status = res.data.data
						this.childCommentList.map((item, index) => {
							this.status.twoZanList[index] ? item.updateBy = true : item.updateBy =
								false
							this.status.twoCaiList[index] ? item.updateTime = true : item
								.updateTime = false
							return item
						})
					})
				})
			},
			// 写评论
			writeComment() {
				writeComment({
					sid: this.parentComment.id,
					pid: this.id,
					content: this.writeContent
				}).then(res => {
					console.log(res)
					if (res.data.msg === '评论成功') {
						this.writeContent = ''
						this.$refs.uToast.show({
							type: 'success',
							message: '评论成功'
						})
					} else {
						this.writeContent = ''
						this.$refs.uToast.show({
							type: 'warning',
							message: '网络出了点小问题'
						})
					}
				})
			},
			// 点赞评论
			clickCommentZan(item) {
				console.log("你点了评论赞++", item.id)
				// 默认1是真 0是假
				// updateBy 和updateTime暂时用不上，将查询出来的状态赋值给它
				if (item.updateBy) {
					console.log("点击取消点赞");
					//取消点赞
					societyCommentZanCancel({
						id: item.id
					}).then(res => {
						if (res.data.msg == "取消点赞评论成功") {
							item.updateBy = false
							item.up--
							this.$refs.uToast.show({
								type: 'success',
								message: '取消点赞成功'
							})
						} else {
							this.$refs.uToast.show({
								type: 'warning',
								message: '取消点赞失败'
							})
						}

					})
				} else {
					//点赞
					console.log("点击点赞");
					societyCommentZan({
						id: item.id
					}).then(res => {
						if (res.data.msg == "点赞评论成功") {
							item.updateBy = true
							item.up++
							if (item.updateTime) {
								item.updateTime = false
								item.down--
							}
							this.$refs.uToast.show({
								type: 'success',
								message: '点赞成功'
							})
						} else {
							this.$refs.uToast.show({
								type: 'warning',
								message: '点赞失败'
							})
						}
					})
				}

			},
			// 点cai评论
			clickCommentCai(item) {
				console.log("你点了踩");
				if (item.updateTime) {
					//取消点踩
					societyCommentCaiCancel({
						id: item.id
					}).then(res => {
						if (res.data.msg == "取消点踩评论成功") {
							item.updateTime = false
							item.down--
							this.$refs.uToast.show({
								type: 'success',
								message: '取消点踩成功'
							})
						} else {
							this.$refs.uToast.show({
								type: 'warning',
								message: '取消点踩失败'
							})
						}

					})
				} else {
					//点踩
					societyCommentCai({
						id: item.id
					}).then(res => {
						if (res.data.msg == "点踩评论成功") {
							item.updateTime = true
							item.down++
							if (item.updateBy) {
								item.updateBy = false
								item.up--
							}
							this.$refs.uToast.show({
								type: 'success',
								message: '点踩成功'
							})
						} else {
							this.$refs.uToast.show({
								type: 'warning',
								message: '点踩失败'
							})
						}
					})
				}

			},
		}

	}
</script>

<style scoped lang="scss">

</style>